package cn.com.lms.business.questionnaire.service;

import cn.com.lms.business.learning.persistence.entity.FbLearningStudent;
import cn.com.lms.business.learning.service.LearningPlanService;
import cn.com.lms.business.learning.service.LearningStudentService;
import cn.com.lms.business.questionnaire.persistence.entity.UserQuestionnaire;
import cn.com.lms.business.questionnaire.persistence.repository.UserQuestionnaireRepository;

import com.bnzj.core.persistence.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @Author: 16266
 * @DateTime: 2020/3/24 13:55
 * @Description:
 */
@Service
public class UserQuestionnaireService extends BaseService<UserQuestionnaireRepository, UserQuestionnaire, Long> {


    @Autowired
    private LearningStudentService learningStudentService;

    @Autowired
    private LearningPlanService planService;
    @Autowired
    private UserQuestionnaireService userQuestionnaireService;

    @Autowired
    private UserQuestionnaireOptService userQuestionnaireOptService;
    /**
     * 获取课程问卷题目平均评分列表
     *
     * @param courseId
     * @return
     * @author zhangzhenjiang
     */
    public List<Map<String, Object>> getQuestionnaireSubjectAvgScoreByCourseId(Long courseId){
        return  repository.getQuestionnaireSubjectAvgScoreByCourseId(courseId);
    }

    public Map<String, Object> getTotalQuestionnaireSubjectAvgScoreByCourseId(Long courseId){
        return  repository.getTotalQuestionnaireSubjectAvgScoreByCourseId(courseId);
    }


    @Transactional(rollbackFor = Exception.class)
    public void addUserQuestionaire(FbLearningStudent learningStudent, UserQuestionnaire userQuestionnaire, Long planId) {
        //更新学习计划表
        learningStudentService.save(learningStudent);
        UserQuestionnaire result=  userQuestionnaireService.save(userQuestionnaire);
        userQuestionnaireOptService.batchSave(result,userQuestionnaire.getUserQuestionnaireOptList());
        // 修改学习计划状态为已完成
        planService.updatePlanFinishState(userQuestionnaire.getPlanId());

    }
}
